Skip to content

feat(cloudflare): add cloudflare pack for Workers and Pages deploys - #656

Merged
dmealing merged 10 commits into
mainfrom
fm/mo-pack-a5-cloudflare
Nov 25, 2026
Merged

dmealing merged 10 commits into
mainfrom
fm/mo-pack-a5-cloudflare

Conversation

@dmealing

@dmealing dmealing commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What Changed

  • Adds the packs/cloudflare pack (@metaobjectsdev/pack-cloudflare, 0.1.0, preview, unpublished). It registers deployment.worker (Worker, with or without static assets), deployment.pages, and the Worker's cloudflare.d1, .kv, .r2, .queue and .ratelimit bindings, on top of the delivery and config packs.
  • Adds generators that write wrangler.jsonc (top-level plus one env block per other environment), deploy/cloudflare/wrangler.sh (wrangler run as the deployment's narrow vault token), deploy.sh, sync-secrets.sh, doctor.sh and unexpressed.md. The generators write files only; the scripts talk to Cloudflare and the vault when run.
  • Adds the pack's CHANGELOG.md, README.md, the packs/README.md index row, four golden-output conformance fixtures, five load-error fixtures, and TypeScript tests (coverage, emit, model, script behaviour against stubbed keys-axi and npx). Also adds the workspace entry in bun.lock.
Validation

Risk Assessment

✅ Low: The latest fix rounds (empty deploy token refusal, wranglerVersion option removal, Pages secret-scope refusal narrowed to project and environment) are correct and covered by script-executing tests. I found no defects. The pack is unpublished and preview, and the change touches no core code.

Testing

Baseline scripts/ci-local.sh --affected --strict-toolchains already passed; broad regression is not rerun here. Focused bun test of the cloudflare pack passed 55/55. Three live scenarios were driven: (1) the generated Worker runner deploys with only the vault token and pinned Wrangler, including the same-named token variant; (2) adversarial empty, absent and same-named empty deploy tokens are refused before any Wrangler call; (3) generator boundaries for Pages: two projects accepted, a secret as value node refused. All three passed with evidence transcripts. A real wrangler deploy against a Cloudflare account was not run (no credentials, network to Cloudflare out of scope), so it is untested. Evidence written to the evidence directory: cf-wrangler-runner-transcript.txt, cf-generator-run.log, cf-generated-wrangler.sh.

  • Live validation: ✅ go - 3 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Worker deploy via generated wrangler.sh uses only the vault deploy token and pinned Wrangler (incl. same-named CLOUDFLARE_API_TOKEN vault entry) — local product, synthetic vault values, stub keys-axi/… ✅ pass live cf-wrangler-runner-transcript.txt (s1-happy, s1-samename)
Adversarial: empty, absent, or same-named-empty deploy token stops wrangler.sh before any Wrangler call (no OAuth fallback) — local generated runner, synthetic inputs ✅ pass live cf-wrangler-runner-transcript.txt (s2-empty, s2-absent, s2-samename-empty)
Generator model boundaries for Pages: two projects sharing a Pages environment accepted; a @secret setting given as a value node refused — real pack generator on fixture cloudflare-pages-secrets with… ✅ pass live cf-generator-run.log
Real Wrangler deploy of the generated config to a Cloudflare account ⏸️ untested no No Cloudflare account credentials or network access to the Cloudflare API are available in this workspace; wrangler is not installed in the worktree. Provide a disposable Cloudflare account token and…
Evidence: Live Worker runner and adversarial token transcript
\### scenario log: s1-happy
== s1-happy
keys-axi run names=ACME_WORKERS_TOKEN CLOUDFLARE_ACCOUNT_ID
npx --yes wrangler@4.168.0 deploy --config wrangler.jsonc
  env: CLOUDFLARE_ACCOUNT_ID=value-of-CLOUDFLARE_ACCOUNT_ID
  env: CLOUDFLARE_API_TOKEN=value-of-ACME_WORKERS_TOKEN
exit=0

\### scenario log: s1-samename
== s1-samename
keys-axi run names=CLOUDFLARE_API_TOKEN CLOUDFLARE_ACCOUNT_ID
npx --yes wrangler@4.168.0 deploy --config wrangler.jsonc
  env: CLOUDFLARE_ACCOUNT_ID=value-of-CLOUDFLARE_ACCOUNT_ID
  env: CLOUDFLARE_API_TOKEN=value-of-CLOUDFLARE_API_TOKEN
exit=0

\### scenario log: s2-empty
== s2-empty
keys-axi run names=ACME_WORKERS_TOKEN CLOUDFLARE_ACCOUNT_ID
exit=1
wrangler.sh: the deploy token ACME_WORKERS_TOKEN is empty or is not in the vault scope

\### scenario log: s2-absent
== s2-absent
keys-axi run names=ACME_WORKERS_TOKEN CLOUDFLARE_ACCOUNT_ID
exit=1
wrangler.sh: the deploy token ACME_WORKERS_TOKEN is empty or is not in the vault scope

\### scenario log: s2-samename-empty
== s2-samename-empty
keys-axi run names=CLOUDFLARE_API_TOKEN CLOUDFLARE_ACCOUNT_ID
exit=1
wrangler.sh: the deploy token CLOUDFLARE_API_TOKEN is empty or is not in the vault scope
Evidence: Generator run: Pages two-project accepted, secret-as-value refused
worker files: deploy/cloudflare/deploy.sh deploy/cloudflare/doctor.sh deploy/cloudflare/sync-secrets.sh deploy/cloudflare/unexpressed.md deploy/cloudflare/wrangler.sh wrangler.jsonc
pages two-project: generated 5 files; sync staging line: acme-help-staging staging SEARCH_API_KEY help-site-staging production
pages secret-as-value: REFUSED: [wrangler-config] cloudflare pack: 1 problem(s) in the model:
  - deployment.pages acme::help::siteProduction value.literal key: Search.apiKey is @secret; its value comes from the vault, never from a value node
Evidence: Generated wrangler.sh from the Worker fixture
#!/usr/bin/env bash
# @generated by @metaobjectsdev/pack-cloudflare (cloudflare-wrangler) — hand edits are kept by a three-way merge on regeneration; meta verify --codegen checks only the generated parts.
# Runs wrangler 4.168.0 (WRANGLER_VERSION overrides) for app.service acme::trips::Trips.web, as the narrow
# Cloudflare API token of one environment's deployment. The token and CLOUDFLARE_ACCOUNT_ID are read
# from the vault with keys-axi. The global-key variables (CLOUDFLARE_EMAIL, CLOUDFLARE_API_KEY) are unset
# so Wrangler cannot fall back to them, and so is any CLOUDFLARE_API_TOKEN the shell holds; the vault's token then becomes CLOUDFLARE_API_TOKEN.
#
# Needs: keys-axi (KEYS_AXI overrides where it is) and npx.
#
#   bash deploy/cloudflare/wrangler.sh <environment> <wrangler arguments>...
set -euo pipefail
cd "$(dirname "$0")/../.."

ENVIRONMENT="${1:-}"
[ "$#" -gt 0 ] && shift
WRANGLER_VERSION="${WRANGLER_VERSION:-4.168.0}"
KEYS_AXI="${KEYS_AXI:-$(command -v keys-axi || echo "$HOME/.local/bin/keys-axi")}"

case "$ENVIRONMENT" in
  production)
    VAULT_PROJECT=acme-deploy VAULT_ENV=dev TOKEN=ACME_WORKERS_TOKEN
    ;;
  *) echo 'usage: bash deploy/cloudflare/wrangler.sh <environment> <wrangler arguments>... (environments: production)' >&2; exit 2 ;;
esac

# shellcheck disable=SC2016 # the inner shell expands them: the token never enters this one
exec env -u CLOUDFLARE_EMAIL -u CLOUDFLARE_API_KEY -u CLOUDFLARE_API_TOKEN \
  "$KEYS_AXI" run --project "$VAULT_PROJECT" --env "$VAULT_ENV" --only "$TOKEN" CLOUDFLARE_ACCOUNT_ID -- \
  bash -c '
    set -euo pipefail
    [ -n "${!1:-}" ] || { echo "wrangler.sh: the deploy token $1 is empty or is not in the vault scope" >&2; exit 1; }
    export CLOUDFLARE_API_TOKEN="${!1}"
    [ "$1" = CLOUDFLARE_API_TOKEN ] || unset "$1"
    : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is not in the vault scope}"
    version="$2"
    shift 2
    exec npx --yes "wrangler@$version" "$@"
  ' wrangler "$TOKEN" "$WRANGLER_VERSION" "$@"

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - all findings resolved (3 fix rounds)

Every finding was fixed in the pipeline's fix rounds; none remain open.

  • ✅ Fixed - packs/cloudflare/typescript/src/files.ts - Pages secret sync and doctor now target the right Pages environment (production or preview, from the project's production branch) with --env; a model that cannot tell is refused, naming the node (CF-1).
  • ✅ Fixed - packs/cloudflare/typescript/src/model.ts - a @secret @phase: build setting and a build-phase value.ref are refused at generation, naming the node and setting, instead of silently dropped; unexpressed.md is accurate for the no-build-command case (CF-2).
  • ✅ Fixed - packs/cloudflare/typescript/src/model.ts - value.ref @path is appended to a publicUrl reference (CF-3).
  • ✅ Fixed - packs/cloudflare/typescript/src/model.ts - inheritable @extra keys are held to the every-environment-or-none rule (CF-4).
  • ✅ Fixed - packs/cloudflare/typescript/src/files.ts - @tokenSecret is quoted in doctor.sh (CF-5).
  • ✅ Fixed - packs/cloudflare/typescript/src/model.ts - unexpressed.md lists a Worker's @branch and only runtime-phase Pages values (CF-6).
  • ✅ Fixed - packs/cloudflare/typescript/src/files.ts - generated heading grammar per kind (CF-7).
  • ✅ Fixed - test fixtures use acme placeholder vault project, scope and token names (CF-8).
  • ✅ Fixed - the shared-Pages-environment refusal applies only to deployments of one Pages project and environment whose secret scopes differ; same-scope deployments and separate projects generate (CF-11, CF-16).
  • ✅ Fixed - wrangler.sh keeps the vault token when the entry is named CLOUDFLARE_API_TOKEN and refuses an empty or missing token, so it never falls back to a logged-in Wrangler session (CF-12, CF-14).
  • ✅ Fixed - the Pages doctor parses Wrangler 4.168.0's real pages secret list output (CF-15).
  • ✅ Fixed - Pages value nodes get the same three checks as Worker value nodes (CF-17).
  • ✅ Fixed - a vault problem is reported once when @tokenVault is absent (CF-18).
  • ✅ Fixed - the wranglerVersion generator option is removed; the runner reads the pinned constant, and the WRANGLER_VERSION environment override stays (CF-19).
  • ✅ Resolved (applied inline, wording only) - doc comment moved onto editFixture; README fixture count corrected (CF-10, CF-13).
  • ✅ Resolved - test-suite coverage gaps noted in the first round were closed by the fix rounds' added tests.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 3 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Worker deploy via generated wrangler.sh uses only the vault deploy token and pinned Wrangler (incl. same-named CLOUDFLARE_API_TOKEN vault entry) — local product, synthetic vault values, stub keys-axi/… ✅ pass live cf-wrangler-runner-transcript.txt (s1-happy, s1-samename)
Adversarial: empty, absent, or same-named-empty deploy token stops wrangler.sh before any Wrangler call (no OAuth fallback) — local generated runner, synthetic inputs ✅ pass live cf-wrangler-runner-transcript.txt (s2-empty, s2-absent, s2-samename-empty)
Generator model boundaries for Pages: two projects sharing a Pages environment accepted; a @secret setting given as a value node refused — real pack generator on fixture cloudflare-pages-secrets with… ✅ pass live cf-generator-run.log
Real Wrangler deploy of the generated config to a Cloudflare account ⏸️ untested no No Cloudflare account credentials or network access to the Cloudflare API are available in this workspace; wrangler is not installed in the worktree. Provide a disposable Cloudflare account token and…
  • scripts/ci-local.sh --affected --strict-toolchains
  • bun test in packs/cloudflare/typescript (55 pass, 0 fail), focused pack suite
  • Generator driver (bun, real emit from packs/cloudflare/typescript/test/model.ts) on fixture cloudflare-worker-cron-ratelimits: wrote deploy/cloudflare/{wrangler.sh,deploy.sh,doctor.sh,sync-secrets.sh,unexpressed.md} and wrangler.jsonc into a scratch site
  • Live run of generated bash deploy/cloudflare/wrangler.sh production deploy --config wrangler.jsonc with stub keys-axi and npx on PATH and synthetic vault values: exit 0, npx ran wrangler@4.168.0 deploy, CLOUDFLARE_API_TOKEN=value-of-ACME_WORKERS_TOKEN, no CLOUDFLARE_EMAIL/CLOUDFLARE_API_KEY
  • Live same-named variant (@tokenSecret CLOUDFLARE_API_TOKEN) with shell token set: exit 0, npx token is the vault value
  • Live adversarial runs: STUB_EMPTY=ACME_WORKERS_TOKEN, STUB_MISSING=ACME_WORKERS_TOKEN, and empty same-named token: exit 1 each, message 'the deploy token ... is empty or is not in the vault scope', npx never invoked
  • Generator refusal/acceptance on cloudflare-pages-secrets with editFixture: two Pages projects (help-site and help-site-staging) sharing Pages production accepted, sync-secrets staging line correct; @secret setting as value.literal refused with the expected message
  • Verified Pages doctor secret-list parser against published wrangler@4.168.0 dist (pagesSecretListCommand prints - NAME: Value Encrypted), via npm pack into scratch
  • Cleanup: scratch dirs removed, worktree clean (git status empty)
✅ **Document** - passed (1 resolved note)
  • ✅ Resolved, no change needed - docs/recipes/cloudflare-workers.md:200 - Out-of-scope follow-up: the recipe's hand-written wrangler.toml section overlaps what packs/cloudflare now generates (wrangler.jsonc, deploy scripts). Still accurate, so left unchanged. When the cloudflare pack leaves preview, reduce that section to a pointer to packs/cloudflare/README.md.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

An independent pack (ADR-0069), 0.1.0 preview, unpublished: deployment.worker
and deployment.pages on the delivery pack's deployment.base, and a Worker's
D1, KV, R2, queue and rate limiting bindings. Generators write wrangler.jsonc
(every env block in full, each fact commented with its model node, valid
against Wrangler 4.168.0's schema), a wrangler wrapper that runs as the
environment's narrow token with the global-key variables unset and a pinned
wrangler, deploy, secret sync and doctor scripts, and the facts no file holds.

Three success fixtures modelled on adopters' hand-written Cloudflare
configuration, with golden output and a fact-by-fact coverage test; five
load-error fixtures. TypeScript port only. Core is unchanged.
…, delivery constants

- deploy.sh sets a build-time default with a shell-quoted value only when the
  variable is unset, so a value holding } or quotes exports as written.
- Refuse @main, @WorkersDev or assets set in some environments and not others:
  Wrangler would fill the gap from the top level.
- An empty value.literal on a build-time setting overrides its @default, as it
  does for a runtime var.
- Read build, runtime and output attributes through the delivery pack's
  constants.
- Name CLOUDFLARE_API_TOKEN apart from the global-key variables in the runner's
  header and the README.
…red test fixtures

- Read the build and runtime processes through the delivery pack's readBuild
  and readProcesses; take MANAGED_BY_EXTERNAL from the systems pack.
- One helper writes each binding list and its comment; the JSONC writer
  refuses a key written twice, so @extra can never shadow a generated key.
- No empty-string sentinel for an unresolved top level; the inherited-key
  check no longer waits on it.
- resolvePaths fills every path; header is module-private.
- Tests load and emit each fixture once.
@dmealing
dmealing merged commit 85f5e6c into main Nov 25, 2026
11 checks passed
@dmealing
dmealing deleted the fm/mo-pack-a5-cloudflare branch November 25, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant